package
{
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	
	import util.PermutationUtil;
	
	public class AnswersCollection extends ArrayCollection
	{
		public function AnswersCollection(answers:XMLList)
		{
			super(null);
			
			var len:Number=3;
			
			var a:ArrayCollection = PermutationUtil.selectedCollection(answers, answers.length());
			
			//wow! what a casting...
			if (!existsACorrectAnswer(new ArrayCollection(new XMLListCollection(answers).toArray())))
				throw new Error("Non esiste nessuna risposta giusta per:" + answers.text());
			
			do 
			{
				a= PermutationUtil.selectedCollection(answers,3);
				
			} while (!existsACorrectAnswer(a));
			
			
			
			this.addAll(a);
			
		}
		
		public static function existsACorrectAnswer(answers:ArrayCollection):Boolean
		{
			for each ( var answer:XML in answers)
			{
				if (answer.@correct == 'true' ) return true;
			}	
			
			return false;
			
		}
		
	}
}